<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActionController::Assertions::RoutingAssertions</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActionController::Assertions::RoutingAssertions</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/actionpack/lib/action_controller/assertions/routing_assertions_rb.html">
                vendor/rails/actionpack/lib/action_controller/assertions/routing_assertions.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Suite of assertions to test routes generated by <a
href="../../Rails.html">Rails</a> and the handling of requests made to
them.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000367">assert_generates</a>&nbsp;&nbsp;
      <a href="#M000366">assert_recognizes</a>&nbsp;&nbsp;
      <a href="#M000368">assert_routing</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000367" class="method-detail">
        <a name="M000367"></a>

        <div class="method-heading">
          <a href="#M000367" class="method-signature">
          <span class="method-name">assert_generates</span><span class="method-args">(expected_path, options, defaults={}, extras = {}, message=nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Asserts that the provided options can be used to generate the provided
path. This is the inverse of <a
href="RoutingAssertions.html#M000366">assert_recognizes</a>. The
<tt>extras</tt> parameter is used to tell the request the names and values
of additional request parameters that would be in a query string. The
<tt>message</tt> parameter allows you to specify a custom error message for
assertion failures.
</p>
<p>
The <tt>defaults</tt> parameter is unused.
</p>
<h4>Examples</h4>
<pre>
  # Asserts that the default action is generated for a route with no action
  assert_generates(&quot;/items&quot;, :controller =&gt; &quot;items&quot;, :action =&gt; &quot;index&quot;)

  # Tests that the list action is properly routed
  assert_generates(&quot;/items/list&quot;, :controller =&gt; &quot;items&quot;, :action =&gt; &quot;list&quot;)

  # Tests the generation of a route with a parameter
  assert_generates(&quot;/items/list/1&quot;, { :controller =&gt; &quot;items&quot;, :action =&gt; &quot;list&quot;, :id =&gt; &quot;1&quot; })

  # Asserts that the generated route gives us our custom route
  assert_generates &quot;changesets/12&quot;, { :controller =&gt; 'scm', :action =&gt; 'show_diff', :revision =&gt; &quot;12&quot; }
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000367-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000367-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/assertions/routing_assertions.rb, line 80</span>
80:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">assert_generates</span>(<span class="ruby-identifier">expected_path</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">defaults</span>={}, <span class="ruby-identifier">extras</span> = {}, <span class="ruby-identifier">message</span>=<span class="ruby-keyword kw">nil</span>)
81:         <span class="ruby-identifier">clean_backtrace</span> <span class="ruby-keyword kw">do</span> 
82:           <span class="ruby-identifier">expected_path</span> = <span class="ruby-node">&quot;/#{expected_path}&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">expected_path</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">==</span> <span class="ruby-value">?/</span>
83:           <span class="ruby-comment cmt"># Load routes.rb if it hasn't been loaded.</span>
84:           <span class="ruby-constant">ActionController</span><span class="ruby-operator">::</span><span class="ruby-constant">Routing</span><span class="ruby-operator">::</span><span class="ruby-constant">Routes</span>.<span class="ruby-identifier">reload</span> <span class="ruby-keyword kw">if</span> <span class="ruby-constant">ActionController</span><span class="ruby-operator">::</span><span class="ruby-constant">Routing</span><span class="ruby-operator">::</span><span class="ruby-constant">Routes</span>.<span class="ruby-identifier">empty?</span> 
85:       
86:           <span class="ruby-identifier">generated_path</span>, <span class="ruby-identifier">extra_keys</span> = <span class="ruby-constant">ActionController</span><span class="ruby-operator">::</span><span class="ruby-constant">Routing</span><span class="ruby-operator">::</span><span class="ruby-constant">Routes</span>.<span class="ruby-identifier">generate_extras</span>(<span class="ruby-identifier">options</span>, <span class="ruby-identifier">defaults</span>)
87:           <span class="ruby-identifier">found_extras</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">reject</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-operator">!</span> <span class="ruby-identifier">extra_keys</span>.<span class="ruby-identifier">include?</span> <span class="ruby-identifier">k</span>}
88: 
89:           <span class="ruby-identifier">msg</span> = <span class="ruby-identifier">build_message</span>(<span class="ruby-identifier">message</span>, <span class="ruby-value str">&quot;found extras &lt;?&gt;, not &lt;?&gt;&quot;</span>, <span class="ruby-identifier">found_extras</span>, <span class="ruby-identifier">extras</span>)
90:           <span class="ruby-identifier">assert_block</span>(<span class="ruby-identifier">msg</span>) { <span class="ruby-identifier">found_extras</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">extras</span> }
91:       
92:           <span class="ruby-identifier">msg</span> = <span class="ruby-identifier">build_message</span>(<span class="ruby-identifier">message</span>, <span class="ruby-value str">&quot;The generated path &lt;?&gt; did not match &lt;?&gt;&quot;</span>, <span class="ruby-identifier">generated_path</span>, 
93:               <span class="ruby-identifier">expected_path</span>)
94:           <span class="ruby-identifier">assert_block</span>(<span class="ruby-identifier">msg</span>) { <span class="ruby-identifier">expected_path</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">generated_path</span> }
95:         <span class="ruby-keyword kw">end</span>
96:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000366" class="method-detail">
        <a name="M000366"></a>

        <div class="method-heading">
          <a href="#M000366" class="method-signature">
          <span class="method-name">assert_recognizes</span><span class="method-args">(expected_options, path, extras={}, message=nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Asserts that the routing of the given <tt>path</tt> was handled correctly
and that the parsed options (given in the <tt>expected_options</tt> hash)
match <tt>path</tt>. Basically, it asserts that <a
href="../../Rails.html">Rails</a> recognizes the route given by
<tt>expected_options</tt>.
</p>
<p>
Pass a hash in the second argument (<tt>path</tt>) to specify the request
method. This is useful for routes requiring a specific HTTP method. The
hash should contain a :path with the incoming request path and a :method
containing the required HTTP verb.
</p>
<pre>
  # assert that POSTing to /items will call the create action on ItemsController
  assert_recognizes({:controller =&gt; 'items', :action =&gt; 'create'}, {:path =&gt; 'items', :method =&gt; :post})
</pre>
<p>
You can also pass in <tt>extras</tt> with a hash containing URL parameters
that would normally be in the query string. This can be used to assert that
values in the query string string will end up in the params hash correctly.
To test query strings you must use the extras argument, appending the query
string on the path directly will not work. For example:
</p>
<pre>
  # assert that a path of '/items/list/1?view=print' returns the correct options
  assert_recognizes({:controller =&gt; 'items', :action =&gt; 'list', :id =&gt; '1', :view =&gt; 'print'}, 'items/list/1', { :view =&gt; &quot;print&quot; })
</pre>
<p>
The <tt>message</tt> parameter allows you to pass in an error message that
is displayed upon failure.
</p>
<h4>Examples</h4>
<pre>
  # Check the default route (i.e., the index action)
  assert_recognizes({:controller =&gt; 'items', :action =&gt; 'index'}, 'items')

  # Test a specific action
  assert_recognizes({:controller =&gt; 'items', :action =&gt; 'list'}, 'items/list')

  # Test an action with a parameter
  assert_recognizes({:controller =&gt; 'items', :action =&gt; 'destroy', :id =&gt; '1'}, 'items/destroy/1')

  # Test a custom route
  assert_recognizes({:controller =&gt; 'items', :action =&gt; 'show', :id =&gt; '1'}, 'view/item1')

  # Check a Simply RESTful generated route
  assert_recognizes(list_items_url, 'items/list')
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000366-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000366-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/assertions/routing_assertions.rb, line 39</span>
39:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">assert_recognizes</span>(<span class="ruby-identifier">expected_options</span>, <span class="ruby-identifier">path</span>, <span class="ruby-identifier">extras</span>={}, <span class="ruby-identifier">message</span>=<span class="ruby-keyword kw">nil</span>)
40:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">path</span>.<span class="ruby-identifier">is_a?</span> <span class="ruby-constant">Hash</span>
41:           <span class="ruby-identifier">request_method</span> = <span class="ruby-identifier">path</span>[<span class="ruby-identifier">:method</span>]
42:           <span class="ruby-identifier">path</span>           = <span class="ruby-identifier">path</span>[<span class="ruby-identifier">:path</span>]
43:         <span class="ruby-keyword kw">else</span>
44:           <span class="ruby-identifier">request_method</span> = <span class="ruby-keyword kw">nil</span>
45:         <span class="ruby-keyword kw">end</span>
46: 
47:         <span class="ruby-identifier">clean_backtrace</span> <span class="ruby-keyword kw">do</span> 
48:           <span class="ruby-constant">ActionController</span><span class="ruby-operator">::</span><span class="ruby-constant">Routing</span><span class="ruby-operator">::</span><span class="ruby-constant">Routes</span>.<span class="ruby-identifier">reload</span> <span class="ruby-keyword kw">if</span> <span class="ruby-constant">ActionController</span><span class="ruby-operator">::</span><span class="ruby-constant">Routing</span><span class="ruby-operator">::</span><span class="ruby-constant">Routes</span>.<span class="ruby-identifier">empty?</span> 
49:           <span class="ruby-identifier">request</span> = <span class="ruby-identifier">recognized_request_for</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">request_method</span>)
50:       
51:           <span class="ruby-identifier">expected_options</span> = <span class="ruby-identifier">expected_options</span>.<span class="ruby-identifier">clone</span>
52:           <span class="ruby-identifier">extras</span>.<span class="ruby-identifier">each_key</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">key</span><span class="ruby-operator">|</span> <span class="ruby-identifier">expected_options</span>.<span class="ruby-identifier">delete</span> <span class="ruby-identifier">key</span> } <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">extras</span>.<span class="ruby-identifier">nil?</span>
53:       
54:           <span class="ruby-identifier">expected_options</span>.<span class="ruby-identifier">stringify_keys!</span>
55:           <span class="ruby-identifier">routing_diff</span> = <span class="ruby-identifier">expected_options</span>.<span class="ruby-identifier">diff</span>(<span class="ruby-identifier">request</span>.<span class="ruby-identifier">path_parameters</span>)
56:           <span class="ruby-identifier">msg</span> = <span class="ruby-identifier">build_message</span>(<span class="ruby-identifier">message</span>, <span class="ruby-value str">&quot;The recognized options &lt;?&gt; did not match &lt;?&gt;, difference: &lt;?&gt;&quot;</span>, 
57:               <span class="ruby-identifier">request</span>.<span class="ruby-identifier">path_parameters</span>, <span class="ruby-identifier">expected_options</span>, <span class="ruby-identifier">expected_options</span>.<span class="ruby-identifier">diff</span>(<span class="ruby-identifier">request</span>.<span class="ruby-identifier">path_parameters</span>))
58:           <span class="ruby-identifier">assert_block</span>(<span class="ruby-identifier">msg</span>) { <span class="ruby-identifier">request</span>.<span class="ruby-identifier">path_parameters</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">expected_options</span> }
59:         <span class="ruby-keyword kw">end</span>
60:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000368" class="method-detail">
        <a name="M000368"></a>

        <div class="method-heading">
          <a href="#M000368" class="method-signature">
          <span class="method-name">assert_routing</span><span class="method-args">(path, options, defaults={}, extras={}, message=nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Asserts that path and options match both ways; in other words, it verifies
that <tt>path</tt> generates <tt>options</tt> and then that
<tt>options</tt> generates <tt>path</tt>. This essentially combines <a
href="RoutingAssertions.html#M000366">assert_recognizes</a> and <a
href="RoutingAssertions.html#M000367">assert_generates</a> into one step.
</p>
<p>
The <tt>extras</tt> hash allows you to specify options that would normally
be provided as a query string to the action. The <tt>message</tt> parameter
allows you to specify a custom error message to display upon failure.
</p>
<h4>Examples</h4>
<pre>
 # Assert a basic route: a controller with the default action (index)
 assert_routing('/home', :controller =&gt; 'home', :action =&gt; 'index')

 # Test a route generated with a specific controller, action, and parameter (id)
 assert_routing('/entries/show/23', :controller =&gt; 'entries', :action =&gt; 'show', id =&gt; 23)

 # Assert a basic route (controller + default action), with an error message if it fails
 assert_routing('/store', { :controller =&gt; 'store', :action =&gt; 'index' }, {}, {}, 'Route for store index not generated properly')

 # Tests a route, providing a defaults hash
 assert_routing 'controller/action/9', {:id =&gt; &quot;9&quot;, :item =&gt; &quot;square&quot;}, {:controller =&gt; &quot;controller&quot;, :action =&gt; &quot;action&quot;}, {}, {:item =&gt; &quot;square&quot;}
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000368-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000368-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/assertions/routing_assertions.rb, line 117</span>
117:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">assert_routing</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">defaults</span>={}, <span class="ruby-identifier">extras</span>={}, <span class="ruby-identifier">message</span>=<span class="ruby-keyword kw">nil</span>)
118:         <span class="ruby-identifier">assert_recognizes</span>(<span class="ruby-identifier">options</span>, <span class="ruby-identifier">path</span>, <span class="ruby-identifier">extras</span>, <span class="ruby-identifier">message</span>)
119:         
120:         <span class="ruby-identifier">controller</span>, <span class="ruby-identifier">default_controller</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:controller</span>], <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:controller</span>] 
121:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">controller</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">controller</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-value">?/</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">default_controller</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">default_controller</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-value">?/</span>)
122:           <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:controller</span>] = <span class="ruby-node">&quot;/#{controller}&quot;</span>
123:         <span class="ruby-keyword kw">end</span>
124:          
125:         <span class="ruby-identifier">assert_generates</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">defaults</span>, <span class="ruby-identifier">extras</span>, <span class="ruby-identifier">message</span>)
126:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>